package com.ds.smartstudio.controller;

import com.ds.smartstudio.common.Constants;
import com.ds.smartstudio.common.Result;
import com.ds.smartstudio.config.MyMQTTClient;
import com.ds.smartstudio.controller.DTO.SearchValues;
import com.ds.smartstudio.entity.Device;
import com.ds.smartstudio.service.DeviceService;
import com.ds.smartstudio.utils.DetectionUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangsf
 * @since 2022-07-02 10:42:24
 */
@RestController
@RequestMapping("/smartstudio/device")
@Slf4j
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @Autowired
    private MyMQTTClient myMQTTClient;

    @ApiOperation("device实体")
    @RequestMapping(method = RequestMethod.HEAD)
    public Device device() {
        return new Device();
    }

    @ApiOperation(value = "分页查询", notes = "根据页码和页面大小和name查询device，模糊匹配device的name，name在不搜索时为空字符串（默认），此方法可以在搜索功能和分页展示上使用，返回一个数组,第一个元素是当页所需要的user数组,第二个元素是当前条件的总记录数")
    @GetMapping
    public List<Object> selectPage(@ApiParam(value = "当前页码", required = true)
                             @RequestParam int page,
                                   @ApiParam(value = "页面大小", required = true)
                             @RequestParam int pageSize,
                                   @ApiParam("设备名称")
                             @RequestParam(defaultValue = "")  String name) {
        return deviceService.devicePage(page, pageSize, name);
    }


    @ApiOperation("调用此方法可以禁用设备（目前只是更改数据库里面的值），返回一个bool值")
    @PatchMapping
    public Boolean disabled(@ApiParam("需要传入一个Device实体") @RequestBody Device device) {
        return deviceService.disabled(device);
    }
    @ApiOperation("调用此方法可以将设备上电（目前只是更改数据库里面的值），返回一个bool值")
    @PatchMapping("/change")
    public Boolean change(@ApiParam("需要传入一个Device实体") @RequestBody Device device) {
        return deviceService.change(device);
    }

    @ApiOperation("该方法用于添加设备，返回一个bool值")
    @PostMapping
    public Boolean addDevice(@ApiParam(value = "需要一个Device实体", required = true) @RequestBody Device device) {
        return deviceService.save(device);
    }

    @ApiOperation("该方法用于编辑设备，返回一个bool值")
    @PutMapping
    public Boolean updateDevice(@ApiParam(value = "需要一个device实体", required = true) @RequestBody Device device) {
         return deviceService.updateById(device);
    }


    @ApiOperation("该方法用于删除设备，返回一个bool值")
    @DeleteMapping
    public Boolean deleteDevice(@ApiParam(value = "需要一个device实体", required = true) @RequestBody Device device) {
        return deviceService.removeById(device);
    }

    @ApiOperation("该方法用于统计计数")
    @GetMapping("/count")
    public Result countDevice() {
        Result result;
        try {
            Integer cnt = deviceService.selectTotalByName("");
            result = Result.succeed(cnt);
        } catch (Exception e) {
            result = Result.error(Constants.CODE_500, e.toString());
        }
        return result;
    }

     @GetMapping("/selectAreaIDByDeviceTypeID")
    public HashSet<String> selectAreaIDByDeviceTypeID(Integer type){
         HashSet<String> area=deviceService.selectAreaIDByDeviceTypeID(type);
        return area;
    }

    @DeleteMapping("/removeDeviceAll")
    public Boolean removeAll(@ApiParam("批量删除") @RequestBody List<Integer> ids) {
        return deviceService.removeBatchByIds(ids);
    }

    @ApiOperation("该方法用于远程重启设备")
    @GetMapping("/rebootDevice")
    public void rebootDevice(@RequestParam String data) {
        //发布消息  topic2 是你要发送到那个通道里面的主题 比如我要发送到topic2主题消息
        myMQTTClient.publish("1", "dreamstudio/"+data+"/cmd");
    }

    @ApiOperation(value = "设备分页查询", notes = "根据页码和页面大小和searchValues查询device，searchValues在不搜索时为空字符串（默认），此方法可以在搜索功能和分页展示上使用，返回一个数组,第一个元素是当页所需要的user数组,第二个元素是当前条件的总记录数")
    @PostMapping("/searchDeviceBySearchValues")
    public List<Object> searchDeviceBySearchValue(@ApiParam(value = "页面数据数", required = true)
                                                  @RequestBody SearchValues searchValues) {
        return deviceService.searchDevice(searchValues);
    }
    @ApiOperation("查询状态值为1")
    @GetMapping("/getList")
   public List<Object> getList(@RequestParam int page, @RequestParam int pageSize ,@RequestParam(defaultValue = "") String name ){

        return deviceService.getList(page, pageSize,name);
   }

   @PostMapping("/setTime")
   public void setTime(@RequestBody List<Date> list) {
       System.out.println(list);
       DetectionUtil.from = list.get(0);
       DetectionUtil.to = list.get(1);
       DetectionUtil.i++;
       DetectionUtil.run();
    }
}
